package campus.rest;

import me.zhengjie.aop.log.Log;
import campus.domain.CampusSystemModule;
import campus.service.CampusSystemModuleService;
import campus.service.dto.CampusSystemModuleQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Api(tags = "模块表管理")
@RestController
@RequestMapping("/api/campusSystemModule")
public class CampusSystemModuleController {

    private final CampusSystemModuleService campusSystemModuleService;

    public CampusSystemModuleController(CampusSystemModuleService campusSystemModuleService) {
        this.campusSystemModuleService = campusSystemModuleService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('campusSystemModule:list')")
    public void download(HttpServletResponse response, CampusSystemModuleQueryCriteria criteria) throws IOException {
        campusSystemModuleService.download(campusSystemModuleService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询模块表")
    @ApiOperation("查询模块表")
    @PreAuthorize("@el.check('campusSystemModule:list')")
    public ResponseEntity<Object> getCampusSystemModules(CampusSystemModuleQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(campusSystemModuleService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增模块表")
    @ApiOperation("新增模块表")
    @PreAuthorize("@el.check('campusSystemModule:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CampusSystemModule resources){
        return new ResponseEntity<>(campusSystemModuleService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改模块表")
    @ApiOperation("修改模块表")
    @PreAuthorize("@el.check('campusSystemModule:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CampusSystemModule resources){
        campusSystemModuleService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除模块表")
    @ApiOperation("删除模块表")
    @PreAuthorize("@el.check('campusSystemModule:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        campusSystemModuleService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}